import {
  deleteContent,down,recall,submit,submit3C
} from './commissionsDistListServ'
// import { parse } from 'qs'
import { message} from 'antd'
// import moment from 'moment'
import {isEmpty, isArray, cloneDeep, merge} from 'lodash'
// import { hashHistory } from 'dva/router'
// import { getProduct } from './commissionsListServ.js'
// import { makeAddress,download } from "utils/util"
import config from 'config/config'
import $ from 'jquery';

const defultState = {
    biz:{
      queryForm: {}
    },
    submitLoading: false,
    submitFeikongLoading: false,
    backLoading: false,
    loading: false,
    products:[],
    id:"",
   
}

const tmpModule = {
  state: cloneDeep(defultState),
  subscriptions: {
    setup ({dispatch, history}) {
      history.listen(({ pathname, query }) => {
        if('' + tmpModule.pathname === '' + pathname){
          dispatch({type: 'updateStore', payload: cloneDeep(defultState)})
        }
      })
    },
  },


  effects: {
    *deleteContent({payload},{put,select,call}){
      let {id} = yield select(state => state[tmpModule.namespace])
      let params = {
        id: id
      }
      const { success, msg, data } = yield call(deleteContent, {...params})
      let defultState = yield select(state => state[tmpModule.namespace])
      if(success){
        message.success('删除成功')
        defultState.biz.refreshListDatas(true)
      }
    },
     
     *down({payload}, {put, call, select}) {
        let {biz,id} = yield select(state => state[tmpModule.namespace])
        let params = {
          id: id,
          ...biz.queryForm 
        }
        const {data, success} = yield call(down,params)
        if (success) {
          var $a = $("<a>");
          $a.attr("href", data.href);
          $a.attr("download", '佣金申请单明细');
          $("body").append($a);
          $a[0].click();
          $a.remove();
          message.success('导出成功,正在为您下载表格')
        }
    },

    *recall({payload},{put,select,call}){
      let {id} = yield select(state => state[tmpModule.namespace])
      yield put({type: 'updateStore', payload: {backLoading: true,loading:true}})
      let params = {
        id: id,
        tab: 2
      }
      const { success, msg, data } = yield call(recall, {...params})
      let defultState = yield select(state => state[tmpModule.namespace])
      if(msg){
        yield put({type: 'updateStore', payload: {backLoading: false,loading:false}})
      }
      if(success){
        message.success('撤回成功')
        defultState.biz.refreshListDatas(true)
      }
    },
    *check({payload},{put,select,call}){
      let {id} = yield select(state => state[tmpModule.namespace])
      // let url = `http://c3uat.midea.com/workflow/request/ViewRequest.jsp?requestid=${id}`
      let url = ''
      if(config.ENV === 'prod'){
        url = `http://c3.midea.com/workflow/request/ViewRequest.jsp?requestid=${id}`
      }else{
        url = `http://c3uat.midea.com/workflow/request/ViewRequest.jsp?requestid=${id}`
      }
      window.open(url)
     
    },
    *submit({payload},{put,select,call}){
      let {id} = yield select(state => state[tmpModule.namespace])
      yield put({type: 'updateStore', payload: {submitFeikongLoading: true,loading:true}})
      let params = {
        id: id
      }
      const { success, msg, data } = yield call(submit, {...params})
      let defultState = yield select(state => state[tmpModule.namespace])
      if(msg){
        yield put({type: 'updateStore', payload: {submitFeikongLoading: false,loading:false}})
      }
      if(success){
        message.success('提交费控成功')
        defultState.biz.refreshListDatas(true)
      }
    },
    *submit3C({payload},{put,select,call}){
      let {id} = yield select(state => state[tmpModule.namespace])
      yield put({type: 'updateStore', payload: {submitLoading: true,loading:true}})
      let params = {
        id: id,
        tab: 2
      }
      const { success, msg, data } = yield call(submit3C, {...params})
      let defultState = yield select(state => state[tmpModule.namespace])
      if(msg){
        yield put({type: 'updateStore', payload: {submitLoading: false,loading:false}})
      }
      if(success){
        message.success('提交3C流程成功')
        defultState.biz.refreshListDatas(true)
      }
    },
    
  },
  // 存入状态机
  reducers: {
    updateStore(preState, action) {
      return Object.assign({}, preState, action.payload)
    },
  }
}

export default tmpModule
